//
//  NestedSubViewController.swift
//  happySwift
//
//  Created by dfsx6 on 2021/6/10.
//  Copyright © 2021 slardar. All rights reserved.
//


import UIKit
import JXSegmentedView
import JXPagingView

extension DSPagingListContainerView: JXSegmentedViewListContainer {}
extension JXPagingListContainerView: JXSegmentedViewListContainer {}

class NestedSubViewController: UIViewController {
//    lazy var pagingView: DSPagingView = preferredPagingView()
//    lazy var userHeaderView: DSPagingViewTableHeaderView = preferredTableHeaderView()
    
    lazy var pagingView: JXPagingView = {
        return JXPagingView.init(delegate: self)
    }()
    
    
    
    lazy var userHeaderView: DSPagingViewTableHeaderView = preferredTableHeaderView()

    
    let dataSource: JXSegmentedTitleDataSource = JXSegmentedTitleDataSource()
    lazy var segmentedView: JXSegmentedView = JXSegmentedView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: CGFloat(headerInSectionHeight)))
    var titles = ["能力", "爱好", "队友"]
    var tableHeaderViewHeight: Int = 200
    var headerInSectionHeight: Int = 50
    var isNeedHeader = false
    var isNeedFooter = false
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.navigationController?.navigationBar.isTranslucent = false
        
        dataSource.titles = titles
        dataSource.titleSelectedColor = UIColor(red: 105/255, green: 144/255, blue: 239/255, alpha: 1)
        dataSource.titleNormalColor = UIColor.black
        dataSource.isTitleColorGradientEnabled = true
        dataSource.isTitleZoomEnabled = true
        
        segmentedView.backgroundColor = UIColor.white
        segmentedView.delegate = self
        segmentedView.isContentScrollViewClickTransitionAnimationEnabled = false
        segmentedView.dataSource = dataSource
        
        let lineView = JXSegmentedIndicatorLineView()
        lineView.indicatorColor = UIColor(red: 105/255, green: 144/255, blue: 239/255, alpha: 1)
        lineView.indicatorWidth = 30
        segmentedView.indicators = [lineView]
        
        let lineWidth = 1/UIScreen.main.scale
        let bottomLineView = UIView()
        bottomLineView.backgroundColor = UIColor.lightGray
        bottomLineView.frame = CGRect(x: 0, y: segmentedView.bounds.height - lineWidth, width: segmentedView.bounds.width, height: lineWidth)
        bottomLineView.autoresizingMask = .flexibleWidth
        segmentedView.addSubview(bottomLineView)
        
        //        pagingView.mainTableView.gestureDelegate = self
        self.view.addSubview(pagingView)
        
        segmentedView.listContainer = pagingView.listContainerView
        
        //扣边返回处理，下面的代码要加上
//        pagingView.listContainerView.scrollView.panGestureRecognizer.require(toFail: self.navigationController!.interactivePopGestureRecognizer!)
//        pagingView.mainTableView.panGestureRecognizer.require(toFail: self.navigationController!.interactivePopGestureRecognizer!)
        
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        self.navigationController?.interactivePopGestureRecognizer?.isEnabled = (segmentedView.selectedIndex == 0)
        
        let button = UIButton.init()
        button.frame = CGRect.init(x: 0, y: 0, width: 200, height: 200)
        button.backgroundColor = .red
        button.addTarget(self, action: #selector(btnAction(_:)), for: .touchUpInside)
        view.addSubview(button)
    }
    
    @objc func btnAction(_ sender: Any?) {
        print(self, self.navigationController)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        
        self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
        pagingView.frame = self.view.bounds
    }
    
    func preferredTableHeaderView() -> DSPagingViewTableHeaderView {
        return DSPagingViewTableHeaderView()
    }
    
//    func preferredPagingView() -> DSPagingView {
//        return DSPagingView(delegate: self)
//    }
}

//extension NestedSubViewController: DSPagingViewDelegate {
//
//    func tableHeaderViewHeight(in pagingView: DSPagingView) -> Int {
//        return tableHeaderViewHeight
//    }
//
//    func tableHeaderView(in pagingView: DSPagingView) -> UIView {
//        return userHeaderView
//    }
//
//    func heightForPinSectionHeader(in pagingView: DSPagingView) -> Int {
//        return headerInSectionHeight
//    }
//
//    func viewForPinSectionHeader(in pagingView: DSPagingView) -> UIView {
//        return segmentedView
//    }
//
//
//
//    func numberOfLists(in pagingView: DSPagingView) -> Int {
//        return titles.count
//    }
//
//    func pagingView(_ pagingView: DSPagingView, initListAtIndex index: Int) -> DSPagingViewListViewDelegate {
//        let list = NestedListViewController.init(style: .grouped)
//        //        list.title = titles[index]
//        //        list.isNeedHeader = isNeedHeader
//        //        list.isNeedFooter = isNeedFooter
//        //        if index == 0 {
//        //            list.dataSource = ["橡胶火箭", "橡胶火箭炮", "橡胶机关枪", "橡胶子弹", "橡胶攻城炮", "橡胶象枪", "橡胶象枪乱打", "橡胶灰熊铳", "橡胶雷神象枪", "橡胶猿王枪", "橡胶犀·榴弹炮", "橡胶大蛇炮", "橡胶火箭", "橡胶火箭炮", "橡胶机关枪", "橡胶子弹", "橡胶攻城炮", "橡胶象枪", "橡胶象枪乱打", "橡胶灰熊铳", "橡胶雷神象枪", "橡胶猿王枪", "橡胶犀·榴弹炮", "橡胶大蛇炮"]
//        //        }else if index == 1 {
//        //            list.dataSource = ["吃烤肉", "吃鸡腿肉", "吃牛肉", "各种肉"]
//        //        }else {
//        //            list.dataSource = ["【剑士】罗罗诺亚·索隆", "【航海士】娜美", "【狙击手】乌索普", "【厨师】香吉士", "【船医】托尼托尼·乔巴", "【船匠】 弗兰奇", "【音乐家】布鲁克", "【考古学家】妮可·罗宾"]
//        //        }
//        return list
//    }
//}

extension NestedSubViewController: JXSegmentedViewDelegate {
    func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
        self.navigationController?.interactivePopGestureRecognizer?.isEnabled = (index == 0)
    }
}

//extension NestedSubViewController: JXPagingMainTableViewGestureDelegate {
//    func mainTableViewGestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
//        //禁止segmentedView左右滑动的时候，上下和左右都可以滚动
//        if otherGestureRecognizer == segmentedView.collectionView.panGestureRecognizer {
//            return false
//        }
//        return gestureRecognizer.isKind(of: UIPanGestureRecognizer.self) && otherGestureRecognizer.isKind(of: UIPanGestureRecognizer.self)
//    }
//}

//MARK:-
class DSPagingViewTableHeaderView: UIView {
    
}

extension NestedSubViewController: JXPagingViewDelegate {
    func pagingView(_ pagingView: JXPagingView, initListAtIndex index: Int) -> JXPagingViewListViewDelegate {
        let list = NestedListViewController.init(style: .grouped)
        return list
    }
    
    func tableHeaderViewHeight(in pagingView: JXPagingView) -> Int {
        return tableHeaderViewHeight
    }
    
    func tableHeaderView(in pagingView: JXPagingView) -> UIView {
        return userHeaderView
    }
    
    func heightForPinSectionHeader(in pagingView: JXPagingView) -> Int {
        return headerInSectionHeight
    }
    
    func viewForPinSectionHeader(in pagingView: JXPagingView) -> UIView {
        return segmentedView
    }
    
    
    
    func numberOfLists(in pagingView: JXPagingView) -> Int {
        return titles.count
    }
    
    
}
